package com.example.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.example.Mapper.CgthDao;
import com.example.pojo.Tuihuocong;
import com.example.pojo.Tuihuozhu;
import com.example.vo.PageBean;
import com.example.vo.TuihuocongVo;
import com.example.vo.TuihuozhuVo;

@Service
public class CgthBiz {
	@Autowired
	private CgthDao cgthdao;

	public CgthDao getCgthdao() {
		return cgthdao;
	}

	public void setCgthdao(CgthDao cgthdao) {
		this.cgthdao = cgthdao;
	}
	/**
	 * 点击保存时，主祥新增
	 * @param save
	 */
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
	public void cgthAdd(Tuihuozhu save){
		if(cgthdao.count(save.getBillno())!=null){
			cgthdao.cgthDelete(save.getBillno());
			cgthdao.cgthAdd(save);
			System.out.println("000");
		}else{
			cgthdao.cgthAdd(save);
			System.out.println("111");
			
		}
		
	}
	/**
	 * 根据单号删除数据
	 * @param billno
	 */
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
	public void cgthDelete(String billno){
		cgthdao.cgthDelete(billno);
		System.out.println(billno);
	}
	/**
	 * 审核修改状态
	 */
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
	public void cgthUpdateA(String billno){
		cgthdao.cgthUpdateA( billno);
	}
	/**
	 * 反审核修改状态
	 */
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
	public void cgthUpdateB(String billno){
		cgthdao.cgthUpdateB( billno);
	}
	/**
	 * 主详加载
	 * @return
	 */
	public PageBean queryPage(int pagenum,int pagesize){
		PageBean pb = new PageBean();
		pb.setPagenum(pagenum);
		pb.setPagesize(pagesize);
		pb.setRowcount(cgthdao.cous());
		//List<Tuihuozhu> list= cgthdao.cgthLoady((pagenum-1)*pagesize,pagesize);
		//List<Tuihuocong> list1 = cgthdao.cgthLoadyCong(list.get(0).getBillno());
		//list.get(0).getTuihuocong().add((Tuihuocong) list1);
		//pb.setPagedata(list);
		//pb.setPagedata(list);
		//System.out.println(list.size());
		List<TuihuozhuVo> zvo = new ArrayList<TuihuozhuVo>();
		List<TuihuocongVo> cvo = new ArrayList<TuihuocongVo>();
		for (Tuihuozhu zpojo : cgthdao.cgthLoady((pagenum-1)*pagesize, pagesize)) {
			List<Tuihuocong> cpojo = cgthdao.cgthLoadyCong(zpojo.getBillno());
			zpojo.setTuihuocong(cpojo);
			for (Tuihuocong tuihuocong : zpojo.getTuihuocong()) {
				TuihuocongVo tcvo = new TuihuocongVo(zpojo.getBillno(), tuihuocong.getProdid(), tuihuocong.getProdname(),
						tuihuocong.getProdsize(), tuihuocong.getEunit(), tuihuocong.getSquantity(),
						tuihuocong.getOldprice(), tuihuocong.getDiscount(), tuihuocong.getSprice(),
						tuihuocong.getAmount(), tuihuocong.getTaxrate(), tuihuocong.getTaxamt(), tuihuocong.getAmountatax(),
						tuihuocong.getNotinvcount(), tuihuocong.getItemremark(), tuihuocong.getTrantype(), 
						tuihuocong.getFromno(), tuihuocong.getZengpin(),tuihuocong.getWeishengxiaosl());
				cvo.add(tcvo);
			}
			TuihuozhuVo tzvo = new TuihuozhuVo(zpojo.getCustid(), zpojo.getCustaddress(), zpojo.getBilldate(), 
					zpojo.getBillno(), zpojo.getPriceoftax(), zpojo.getCurrid(), zpojo.getExchrate(),
					zpojo.getWareid(), zpojo.getIsdeduct(), zpojo.getVoucherno(), zpojo.getSalesid(), zpojo.getZongjine(),
					zpojo.getMaker(), zpojo.getDepartname(), zpojo.getPermitter(), zpojo.getProjectname(),
					zpojo.getBeizhu(), zpojo.getIsshenhe(), zpojo.getZmoneys(), zpojo.getFukuandate(),
					zpojo.getZhangkuanmonth(), cvo);
			zvo.add(tzvo);
		}
		pb.setPagedata(zvo);
		return pb;
		
	}
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
	public void xgdddkUpdate(String Billno){
		cgthdao.xgdddkUpdate(Billno);
	} 
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
	public void xgddwrklUpdate(String Qtyremain,String Billno){
		cgthdao.xgddwrklUpdate(Qtyremain, Billno);
	}
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
	public void xggyszjdate(String gysname){
		cgthdao.xggyszjdate(gysname);
	}
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
	public void xgckcount(double number,String prodId){
		//System.out.println("数量");
		cgthdao.xgckcount(number, prodId);
	}
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
	public void fsxgckcount(double number,String prodId){
		//System.out.println("数量");
		cgthdao.fsxgckcount(number, prodId);
	}
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
	public void xgthwckslUpdate(int weishengxiaosl,String billno){
		cgthdao.xgthwckslUpdate(weishengxiaosl, billno);
	}
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
	public void shxggyszwjxinxi(double qimyingfuk,String GYSname){
		cgthdao.shxggyszwjxinxi(qimyingfuk, GYSname);
	}
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
	public void fshxggyszwjxinxi(double qimyingfuk,String GYSname){
		cgthdao.fshxggyszwjxinxi(qimyingfuk, GYSname);
	}
}
